use strict;
use warnings;
use Time::HiRes qw(sleep);
use Test::WWW::Selenium;
use Test::More "no_plan";
use Test::Exception;

my $sel = Test::WWW::Selenium->new( host => "localhost", 
                                    port => 4444, 
                                    browser => "*firefox", 
                                    browser_url => "http://localhost.com" );

$sel->open_ok("/");
#$sel->click_ok("log_in_top");
$sel->wait_for_page_to_load_ok("30000");
$sel->is_element_present_ok("link=Log out");
$sel->click_ok("link=Administration");
$sel->wait_for_page_to_load_ok("30000");
$sel->click_ok("link=Bug Prediction");
$sel->text_is("link=Change Classification", "Change Classification");
$sel->click_ok("link=Change Classification");
WAIT: {
    for (1..60) {
        if (eval { $sel->is_element_present("classification") }) { pass; last WAIT }
        sleep(1);
    }
    fail("timeout");
}
$sel->text_is("//td[\@id='content']/h3", "Change Classification");
$sel->text_is("//td[\@id='content']/p", "You can change whether to classify using the title, the content or both.");
$sel->select_ok("classification", "label=Title");
$sel->click_ok("classification_update");
WAIT: {
    for (1..60) {
        if (eval { $sel->is_text_present("") }) { pass; last WAIT }
        sleep(1);
    }
    fail("timeout");
}
WAIT: {
    for (1..60) {
        if (eval { !$sel->is_text_present("You can change whether to classify using the title, the content or both.") }) { pass; last WAIT }
        sleep(1);
    }
    fail("timeout");
}
$sel->text_is("//td[\@id='content']/p", "The classification has been changed, please update the bug prediction models!");
$sel->click_ok("link=Change Classification");
WAIT: {
    for (1..60) {
        if (eval { $sel->is_element_present("classification") }) { pass; last WAIT }
        sleep(1);
    }
    fail("timeout");
}
$sel->value_is("classification", "title");
